package artid.com.arttogoa.adapter;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import artid.com.arttogoa.R;
import artid.com.arttogoa.util.CircleImageView;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.VeDate;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.EMMessage.Type;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：InquiryAdapter   
 * 类描述   ：消息里的询价适配器   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-4 上午10:28:29   
 *
 */
public class InquiryAdapter extends BaseAdapter {
	private Context mContext;
	private List<EMConversation> list;
	public InquiryAdapter(Context mContext, List<EMConversation> list) {
		super();
		this.mContext = mContext;
		this.list = list;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_inquiry, null);
			holder.icon=(CircleImageView) v.findViewById(R.id.itemInquiry_icon);
			holder.unreadMessage_icon=(ImageView) v.findViewById(R.id.itemInquiry_unreadMessage);
			holder.name=(TextView) v.findViewById(R.id.itemInquiry_Name);
			holder.time=(TextView) v.findViewById(R.id.itemInquiry_time);
			holder.content=(TextView) v.findViewById(R.id.itemInquiry_content);
			v.setTag(holder);
		}else {
			holder = (ViewHolder) v.getTag();
		}
		String friendAvatar = "";//好友头像
		String fromName="";//好友ID（也是名字）
		holder.time.setText(VeDate.getLnow(list.get(position).getLastMessage().getMsgTime()));
		Type type = list.get(position).getLastMessage().getType();
		if(type==EMMessage.Type.TXT){
			EMTextMessageBody txtBody = (EMTextMessageBody) list.get(position).getLastMessage().getBody();
			holder.content.setText(txtBody.getMessage());
		}else{
			holder.content.setText("/图片/");
		}
		friendAvatar=list.get(position).getLastMessage().getStringAttribute("artistURL","http://172.16.0.3/upload/artPlatform/Addons/Avatar/default_128_128.png");
		holder.icon.setTag(friendAvatar);
		ImageLoader.getInstance().displayImage(friendAvatar
				+ "_"+ NetUtils.dip2px(mContext, 100)+"x"+NetUtils.dip2px(mContext, 100)+".png"
				, holder.icon);
		fromName=list.get(position).getLastMessage().getStringAttribute("fromName",list.get(position).conversationId());
		holder.name.setText(fromName);
		EMConversation conversation = EMClient.getInstance().chatManager().getConversation(list.get(position).conversationId());
		if(conversation.getUnreadMsgCount()!=0){
			holder.unreadMessage_icon.setVisibility(View.VISIBLE);
		}else{
			holder.unreadMessage_icon.setVisibility(View.GONE);
		}
		return v;
	}
	class ViewHolder {
		CircleImageView icon;
		ImageView unreadMessage_icon;
		TextView name,time,content;
	}

}
